﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Configuration;
using System.Configuration;

namespace Marbles.Core.Web.Images
{
    public abstract class HttpHandlerBase
    {

        public static bool IsHttpHandlerRegistered(string type)
        {
            Configuration configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/");
            HttpHandlersSection sysweb = configuration.GetSection("system.web/httpHandlers") as HttpHandlersSection;
            bool fnd = false;

            for (int i = 0; i < sysweb.Handlers.Count; i++)
            {
                if (sysweb.Handlers[i].Type == type)
                {
                    fnd = true; break;
                }
            }

            return fnd;
        }

        public static void RegisterHttpHandler(string path, string type, string verb)
        {
            Configuration configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/");
            HttpHandlersSection sysweb = configuration.GetSection("system.web/httpHandlers") as HttpHandlersSection;
            bool fnd = false;

            for (int i = 0; i < sysweb.Handlers.Count; i++)
            {
                if (sysweb.Handlers[i].Type == type)
                {
                    fnd = true; break;
                }
            }

            if (fnd) { return; }

            HttpHandlerAction hand = new HttpHandlerAction(path, type, verb, false);
            sysweb.Handlers.Add(hand);
            configuration.Save();
        }

        public abstract void Register();

    }
}
